package com.gray.blog.controller.sys;

import com.google.common.collect.Maps;
import com.gray.blog.common.annotation.IpCheckTrigger;
import com.gray.blog.common.controller.BaseController;
import com.gray.blog.common.contstant.NowApiConstant;
import com.gray.blog.common.enums.ErrorCodeEnum;
import com.gray.blog.common.result.ModelResult;
import com.gray.blog.common.util.BeanUtil;
import com.gray.blog.common.util.JsonObjectUtil;
import com.gray.blog.common.util.OkHttpUtil;
import com.gray.blog.dto.nowApi.WeatherTodayResult;
import com.gray.blog.result.sys.GetCityWeatherResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.net.URL;
import java.util.Map;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created  By zhoux 2019/05/10
 **/
@Slf4j
@RestController
@RequestMapping(value = "/open/outer")
@Api(value = "三方接口", description = "三方接口")
@CrossOrigin
public class OuterApiController extends BaseController {

    @Autowired
    private OkHttpUtil okHttpUtil;


    @RequestMapping(value = "/weather/{weaId}/", method = RequestMethod.GET)
    @IpCheckTrigger
    @ApiOperation(value = "获取城市天气")
    public ModelResult<GetCityWeatherResult> getCityWeather(@PathVariable("weaId") String weaId) {
        if (StringUtils.isBlank(weaId)) {
            log.error("OuterApiController.getCityWeather weaId is null");
            return  ModelResult.newError(ErrorCodeEnum.PARAMS_ERROR);
        }
        Map<String, String> param = Maps.newHashMap();
        param.put("app", "weather.today");
        param.put("weaid", weaId);
        param.put("appkey", NowApiConstant.APPKEY);
        param.put("sign", NowApiConstant.SIGN);
        param.put("format", "json");
        String result = okHttpUtil.get(NowApiConstant.NOW_API_URL, param);
        WeatherTodayResult weatherTodayResult =  JsonObjectUtil.getObjectFromJson(result, NowApiConstant.SUCCESS_KEY, NowApiConstant.SUCCESS_VALUE, NowApiConstant.RESULT_KEY, WeatherTodayResult.class);
        GetCityWeatherResult getCityWeatherResult = BeanUtil.copy(weatherTodayResult, GetCityWeatherResult.class);
        return ModelResult.newSuccess(getCityWeatherResult);
    }


    public static void main(String args[]) throws Exception {
        URL u=new URL("http://api.k780.com/?app=weather.wtype&appkey=42354&sign=abfb80918eac0b238d63258b6dd54d3c&format=json");
        InputStream in=u.openStream();
        ByteArrayOutputStream out=new ByteArrayOutputStream();
        try {
            byte buf[]=new byte[1024];
            int read = 0;
            while ((read = in.read(buf)) > 0) {
                out.write(buf, 0, read);
            }
        }  finally {
            if (in != null) {
                in.close();
            }
        }
        byte b[]=out.toByteArray( );
        System.out.println(new String(b,"utf-8"));
    }

}
